<?php
 if(!isset($sp))	
  	$sp=explode("/",$_SERVER['PHP_SELF']);

  if($_SERVER['HTTP_HOST'] == "localhost" || $_SERVER['HTTP_HOST'] == "192.168.1.34")
   {
	  $sp[1] = '/' . $sp[1];
   }
  else
	  $sp[1] = '';
  
  $w_root = $_SERVER['SERVER_NAME'] . $sp[1];
  $w_root = str_replace('//','/',$w_root);
  $w_root = str_replace('\\\\','\\',$w_root);
  
  $s_root = $_SERVER['DOCUMENT_ROOT'] . $sp[1];
  $s_root = str_replace('//','/',$s_root);
  $s_root = str_replace('\\\\','\\',$s_root); 
/*========End Setting up web root and server root =============*/

    $config = $s_root. '/hybridauth/config.php';
    require_once$s_root. '/hybridauth/Hybrid/Auth.php';
    
    
	// we need to know it
	$CURRENT_URL = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
	
	// change the following paths if necessary 


	try{
		$hybridauth = new Hybrid_Auth( $config );
	}
	catch( Exception $e ){
		echo "Ooophs, we got an error: " . $e->getMessage();
	}

	$provider = ""; 
	
	// handle logout request
	if( isset( $_GET["logout"] ) ){
		$provider = $_GET["logout"];

		$adapter = $hybridauth->getAdapter( $provider );

		$adapter->logout();
		
		header( "Location: index.php"  );
		
		die();
	}

	// if the user select a provider and authenticate with it 
	// then the widget will return this provider name in "connected_with" argument 
	elseif( isset( $_GET["connected_with"] ) && $hybridauth->isConnectedWith( $_GET["connected_with"] ) ){
$hybridauth = new Hybrid_Auth( $config );
			header("location:".W_ROOT."../check.php?provider=$provider");
		
		die();
	} // if user connected to the selected provider 

	// if not, include unauthenticated user view
	include "inc_unauthenticated_user.php";
